<template>
  <views class="depart_manager_detail">
    <div>
      <viewTitle :title="'修改组织'" border></viewTitle>
      <div class="depart_manager_detail_input">
        <el-form ref="form" label-width="auto" :inline="true">
          <el-form-item label="部门名称:">
            <el-input type="text" clearable placeholder="请输入部门名称" v-model="department_name"></el-input>
          </el-form-item>
          <el-form-item label="上级部门:">
            <el-cascader
                    v-model="parent_id"
                    :options="department_list"
                    :props="{ checkStrictly: true }"
                    clearable></el-cascader>
          </el-form-item>
        </el-form>
        <div>
              <el-row>
                <el-button type="primary" @click="submit">提交</el-button>
                <el-button @click="reset">重置</el-button>
                <el-button @click="goBlack" type="danger">返回</el-button>
              </el-row>
        </div>
      </div>
    </div>
  </views>
</template>


<script>
export default {
  data() {
    return {
        department_id: 0, //  部门ID
        department_name: "", //  部门名称
        parent_id: "", //  上级部门
        department_list: [],
    };
  },
  methods: {
           // 提交
           submit(){
               console.log('post_data',this.post_data);
               // return
              this.$raxios.post(
                   this.$mapapi.admin_entryDepartment,
                   {data:this.post_data}
               ).then(res=>{
                 if(res.code==200){
                    this.$message({type: "success", message: "修改成功!"});
                    this.goBlack();
                 }else{
                  this.$message.error(res.msg);
                   return;
                 }
               })
           },
           // 重置
           reset(){
               this.parent_id = [];
               this.department_name = "";
           },
           goBlack(){
               this.$router.go(-1)
           }

  },
  computed:{
      post_data(){
          let data = {};
          if(this.department_id > 0){
              data.department_id = this.department_id;
          }
          data.department_name = this.department_name;
          data.parent_id = this.parent_id.slice(-1)[0] || 0;
          return data;
      }
  },
  created() {
      let { code, msg, data } = this.$api.sendSync(
          this.$mapapi.admin_departmentTree
      );
      if (code != 200) {
          this.$message.error(msg);
          return;
      }
      console.log('data',data);
      this.department_list = data;
  },
  mounted() {
      let dep_id = this.$route.query.department_id||0;
      let post_data={
        department_id:dep_id
      }
      if(!dep_id){
          this.$message.error('请传入部门ID');
      }
     
      this.$raxios.post(
          this.$mapapi.admin_detailDepartment,{
              data:post_data
          }
      ).then(res=>{
        if(res.code==200){
          this.department_name = res.data.department_name;
          this.department_id = res.data.department_id;
          this.parent_id = res.data.ids;
        }else{
           this.$message.error(res.msg);
          return;
        }
      })
  }
};
</script>

<style lang="less" scope="true"  src='@/style/system_manager/depart_manager_detail.less'>
</style>